﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using System.Collections.ObjectModel;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class N050_SubForum {
		private IWebDriver driver;
		private String forumName;
		private String subForum1Name;
		private String subForum2Name;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();
			subForum1Name = Guid.NewGuid().ToString();
			subForum2Name = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);

			driver.CreateSubForum("Test board", "Category 1", forumName, subForum1Name, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName, subForum1Name);

			driver.CreateSubForum("Test board", "Category 1", forumName, subForum2Name, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName, subForum2Name);

			driver.DisableAntiSpam();
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void ForumTopicsUpdate() {
			String parentForumTopic = "Parent Forum Topic";
			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);
			driver.CreateTopic(forumName, parentForumTopic, "bla bla");
			driver.LogOff();

			driver.VisitURL("forum");

			Int32 topics;
			Int32 posts;
			String latestTopic;
			String latestAuthor;
			driver.GetForumInfo(forumName, out topics, out posts, out latestTopic, out latestAuthor);

			Assert.AreEqual(1, topics, "We have only posted one topic");
			Assert.AreEqual(0, posts, "No posts have been posted");
			Assert.AreEqual(parentForumTopic, latestTopic, "Not the right topic");
			Assert.AreEqual(Constants.Users.Regular.Username, latestAuthor, "Not the right author");

			String sub1Topic = "Sub Forum 1 Topic";
			driver.LogOn(Constants.Users.Moderator.Email, Constants.Users.Moderator.Password);
			driver.CreateSubForumTopic(forumName, subForum1Name, sub1Topic, "bla bla");
			driver.LogOff();

			driver.VisitURL("forum");

			driver.GetForumInfo(forumName, out topics, out posts, out latestTopic, out latestAuthor);

			Assert.AreEqual(2, topics, "We have only posted two topics");
			Assert.AreEqual(0, posts, "No posts have been posted");
			Assert.AreEqual(sub1Topic, latestTopic, "Not the right topic");
			Assert.AreEqual(Constants.Users.Moderator.Username, latestAuthor, "Not the right author");

			String sub2Topic = "Sub Forum 2 Topic";
			driver.LogOn(Constants.Users.Moderator.Email, Constants.Users.Moderator.Password);
			driver.CreateSubForumTopic(forumName,subForum2Name, sub2Topic, "bla bla");
			driver.LogOff();

			driver.VisitURL("forum");

			driver.GetForumInfo(forumName, out topics, out posts, out latestTopic, out latestAuthor);

			Assert.AreEqual(3, topics, "We have only posted three topics");
			Assert.AreEqual(0, posts, "No posts have been posted");
			Assert.AreEqual(sub2Topic, latestTopic, "Not the right topic");
			Assert.AreEqual(Constants.Users.Moderator.Username, latestAuthor, "Not the right author");
		}

		// TODO:
		//[Test]
		//public void ForumPostsUpdate() {
		//}

		// TODO:
		//[Test]
		//public void ForumPostsDelete() {
		//}
	}
}